home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / handson / supercede / Knowodys / Projects / Rectang / 1.1 / Bounce.java < prev    next >
Encoding:
Java Source  |  1997-10-29  |  2.9 KB  |  163 lines

  1. /*
  2.  *    File:
  3.  *    Bounce.java
  4.  *
  5.  *  Copyright (c) 1996-1997 SuperCede, Inc. All rights reserved.
  6.  *
  7.  */
  8.  
  9. import java.awt.*;
  10. import java.awt.image.*;
  11. import java.applet.*;
  12. import java.net.*;
  13.  
  14. public class Bounce extends Applet implements Runnable
  15. {
  16.     Thread worker = null;
  17.     Callback helper = null;
  18.  
  19.     Graphics shadow_graphics = null;
  20.     Image shadow_image = null;
  21.  
  22.     int x = 0;
  23.     int y = 0;
  24.  
  25.     int x_direction = 1;
  26.     int y_direction = 1;
  27.  
  28.     Dimension window_size = null;
  29.  
  30.     public void setBounds (int x, int y, int width, int height)
  31.     {
  32.         super.setBounds (x, y, width, height);
  33.         window_size = getSize ();
  34.         shadow_image = createImage (window_size.width, window_size.height);
  35.         shadow_graphics = shadow_image.getGraphics ();
  36.         prepareImage (shadow_image, this);
  37.     }
  38.  
  39.     public void draw (Graphics graphics)
  40.     {
  41.         graphics.clearRect (0, 0, window_size.width, window_size.height);
  42.         graphics.setColor (helper.getColor());
  43.         int object_size = helper.getSize();
  44.         switch (helper.getShape ())
  45.         {
  46.             case Callback.Rectangle:
  47.                   graphics.fillRect (x, y, object_size, object_size);
  48.                 break;
  49.  
  50.             case Callback.Circle:
  51.                 graphics.fillOval (x, y, object_size, object_size);
  52.                 break;
  53.  
  54.             default:
  55.                 break;
  56.         }
  57.     }
  58.  
  59.     public void paint (Graphics graphics)
  60.     {
  61.         draw (shadow_graphics);
  62.         graphics.drawImage (shadow_image, 0, 0, null);
  63.     }
  64.  
  65.     public void update (Graphics graphics)
  66.     {
  67.         paint (graphics);
  68.     }
  69.  
  70.     public void start()
  71.     {
  72.         if (worker == null)
  73.         {
  74.             helper = new Callback ();
  75.             worker = new Thread (this);
  76.             worker.start ();
  77.         }
  78.     }
  79.  
  80.     public void stop()
  81.     {
  82.         if (worker != null)
  83.         {
  84.             worker.stop ();
  85.             worker = null;
  86.             helper = null;
  87.         }
  88.     }
  89.  
  90.     public void run ()
  91.     {
  92.         while (true)
  93.         {
  94.  
  95.             //    Advance the ball.
  96.  
  97.             int object_size = helper.getSize ();
  98.             int step_size = helper.getStep ();
  99.  
  100.             x += x_direction * step_size;
  101.             y += y_direction * step_size;
  102.  
  103.             if (x <= 0)
  104.             {
  105.                 x_direction = Math.abs (x_direction);
  106.                 x = 0;
  107.             }
  108.  
  109.             int x_limit = window_size.width - object_size;
  110.             if (x >= x_limit)
  111.             {
  112.                 x_direction = -Math.abs (x_direction);
  113.                 x = x_limit;
  114.             }
  115.  
  116.             if (y <= 0)
  117.             {
  118.                 y_direction = Math.abs (y_direction);
  119.                 y = 0;
  120.             }
  121.  
  122.             int y_limit = window_size.height - object_size;
  123.             if (y >= y_limit)
  124.             {
  125.                 y_direction = -Math.abs (y_direction);
  126.                 y = y_limit;
  127.             }
  128.  
  129.             //    Paint the current image.
  130.  
  131.             repaint ();
  132.  
  133.             //    Wait for some time to pass.
  134.  
  135.             try
  136.             {
  137.                 Thread.sleep (helper.getSleep());
  138.             }
  139.             catch (InterruptedException e)
  140.             {
  141.             }
  142.         }
  143.     }
  144.  
  145.     public static void main (String params [])
  146.     {
  147.         Frame frame = new Application ();
  148.         frame.setTitle ("Bounce");
  149.         frame.setBounds (10, 10, 300, 400);
  150.         frame.addNotify ();
  151.  
  152.         Applet applet = new Bounce ();
  153.         frame.add ("Center", applet);
  154.         applet.addNotify ();
  155.         frame.doLayout ();
  156.  
  157.         applet.init ();
  158.         applet.start ();
  159.  
  160.         frame.show ();
  161.     }
  162. }
  163.